#include "legendarybrowsermainwindow.h"

#include <QAction>

namespace Legenday{
namespace Browser{

MainWindow::MainWindow()
{
	QList<QKeySequence> keys;
	
	m_actionCloseTab = new QAction(this);
	m_actionCloseTab->setShortcut(QKeySequence::Close);
	connect(m_actionCloseTab,SIGNAL(triggered()), this, SLOT(closetab()));
	addAction(m_actionCloseTab);
	
	m_actionPrevTab = new QAction(this);
	keys.clear();
	keys.append(QKeySequence("Ctrl+PgUp"));
	keys.append(QKeySequence("Ctrl+["));
	m_actionPrevTab->setShortcuts(keys);
	connect(m_actionPrevTab,SIGNAL(triggered()), this, SLOT(selectPrevTab()));
	addAction(m_actionPrevTab);
	
	m_actionNextTab = new QAction(this);
	keys.clear();
	keys.append(QKeySequence("Ctrl+PgDown"));
	keys.append(QKeySequence("Ctrl+]"));
	m_actionNextTab->setShortcuts(keys);
	connect(m_actionNextTab,SIGNAL(triggered()), this, SLOT(selectNextTab()));
	addAction(m_actionNextTab);
	
	m_tabWidget = new QTabWidget(this);
	setCentralWidget(m_tabWidget);
	setWindowTitle(tr("Legendary browser"));
}

void	MainWindow::closetab()
{
	if (m_tabWidget->count()==1)
		this->deleteLater();
	m_tabWidget->currentWidget()->deleteLater();
}

void	MainWindow::selectPrevTab()
{
	int i = m_tabWidget->currentIndex() -1;
	if (i==-1)
		i = m_tabWidget->count()-1;
	m_tabWidget->setCurrentIndex(i);
}

void	MainWindow::selectNextTab()
{
	int i = m_tabWidget->currentIndex() +1;
	if (i==m_tabWidget->count())
		i = 0;
	m_tabWidget->setCurrentIndex(i);
}

} // Browser
} // Legendary

